﻿using System.Windows;
using PSE.Deployment.ConfigurationManager.Controls.Validations;
using PSE.Deployment.SetupBase.Configuration;

namespace PSE.Deployment.ConfigurationManager.Controls
{
	/// <summary>
	/// UserControl que permite somente input no formato HH:MM:SS
	/// </summary>
	public class TimeTextBox : BaseSimpleTextBox
	{
		private TimeTextBox()
		{
			this.PreviewDrop += new System.Windows.DragEventHandler(_PreviewDrop);

			DataObject.AddPastingHandler(this, new DataObjectPastingEventHandler(CheckPasteFormat));

			this.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
			this.VerticalAlignment = System.Windows.VerticalAlignment.Center;
		}

		public TimeTextBox(XmlTag tag) : this()
		{
			this.DataContext = tag;
			ConfigurationBinding bd = new ConfigurationBinding();
			this.SetBinding(TimeTextBox.TextProperty, bd);
			this.IsRequired = tag.IsRequired;
		}

		public TimeTextBox(XmlAttribute attribute) : this()
		{
			this.DataContext = attribute;
			ConfigurationBinding bd = new ConfigurationBinding();
			this.SetBinding(TimeTextBox.TextProperty, bd);
			this.IsRequired = attribute.IsRequired;
		}

		public TimeTextBox(RegistryValue reg) : this()
		{
			this.DataContext = reg;
			ConfigurationBinding bd = new ConfigurationBinding();
			this.SetBinding(TimeTextBox.TextProperty, bd);
			this.IsRequired = reg.IsRequired;
		}

		void _PreviewDrop(object sender, System.Windows.DragEventArgs e)
		{
			if (e.Data.GetData(typeof(string)) != null && !SpecializedControlsValidations.IsTime((string)e.Data.GetData(typeof(string)), false))
				e.Handled = true;
		}

		void CheckPasteFormat(object sender, DataObjectPastingEventArgs e)
		{
			if (e.DataObject.GetData(typeof(string)) != null && !SpecializedControlsValidations.IsTime((string)e.DataObject.GetData(typeof(string)), false))
				e.CancelCommand();
		}

        protected override bool Validate()
        {
            return SpecializedControlsValidations.IsTime(this.Text, false);
        }

        protected override ErrorMessage GetErrorMessage()
        {
            return ErrorMessage.InvalidTime;
        }
	}
}